<template>
  <a-modal
    v-model:open="visible"
    title="用户登录"
    :footer="null"
    :width="400"
    centered
    @cancel="handleCancel"
  >
    <div class="login-modal-content">
      <div class="login-header">
        <h3>飞图智能协同云工坊</h3>
        <p>企业级智能协同云图库</p>
      </div>
      
      <a-form 
        :model="formState" 
        name="loginForm" 
        autocomplete="off" 
        @finish="handleSubmit"
        :loading="loading"
      >
        <a-form-item 
          name="userAccount" 
          :rules="[{ required: true, message: '请输入账号' }]"
        >
          <a-input 
            v-model:value="formState.userAccount" 
            placeholder="请输入账号" 
            size="large"
          />
        </a-form-item>
        
        <a-form-item
          name="userPassword"
          :rules="[
            { required: true, message: '请输入密码' },
            { min: 8, message: '密码长度不能小于 8 位' },
          ]"
        >
          <a-input-password 
            v-model:value="formState.userPassword" 
            placeholder="请输入密码" 
            size="large"
          />
        </a-form-item>
        
        <a-form-item>
          <a-button 
            type="primary" 
            html-type="submit" 
            size="large"
            :loading="loading"
            style="width: 100%"
          >
            登录
          </a-button>
        </a-form-item>
      </a-form>
      
      <div class="login-footer">
        <span>没有账号？</span>
        <a @click="goToRegister">去注册</a>
      </div>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { userLoginUsingPost } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

interface Props {
  open: boolean
}

interface Emits {
  (e: 'update:open', value: boolean): void
  (e: 'success'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const router = useRouter()
const loginUserStore = useLoginUserStore()

const visible = ref(false)
const loading = ref(false)

// 表单数据
const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

// 监听props变化
watch(() => props.open, (newVal) => {
  visible.value = newVal
})

// 监听visible变化
watch(visible, (newVal) => {
  emit('update:open', newVal)
  if (!newVal) {
    // 关闭时重置表单
    formState.userAccount = ''
    formState.userPassword = ''
  }
})

// 处理取消
const handleCancel = () => {
  visible.value = false
}

// 提交登录表单
const handleSubmit = async (values: API.UserLoginRequest) => {
  loading.value = true
  try {
    const res = await userLoginUsingPost(values)
    if (res.data.code === 0 && res.data.data) {
      // 登录成功，保存用户信息
      loginUserStore.setLoginUser(res.data.data)
      message.success('登录成功')
      visible.value = false
      emit('success')
    } else {
      message.error('登录失败，' + (res.data.message || '请检查账号密码'))
    }
  } catch (error) {
    console.error('登录出错:', error)
    message.error('登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 去注册页面
const goToRegister = () => {
  visible.value = false
  router.push('/user/register')
}
</script>

<style scoped>
.login-modal-content {
  padding: 1rem 0;
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-header h3 {
  margin: 0 0 0.5rem 0;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--primary-color);
}

.login-header p {
  margin: 0;
  color: #666;
  font-size: 0.875rem;
}

.login-footer {
  text-align: center;
  margin-top: 1rem;
  color: #666;
  font-size: 0.875rem;
}

/* 黑色模式下的登录模态框样式 */
[data-theme="dark"] .login-header p {
  color: #9ca3af;
}

[data-theme="dark"] .login-footer {
  color: #9ca3af;
}

.login-footer a {
  color: var(--primary-color);
  text-decoration: none;
  margin-left: 0.25rem;
}

.login-footer a:hover {
  text-decoration: underline;
}

:deep(.ant-form-item) {
  margin-bottom: 1.5rem;
}

:deep(.ant-input-affix-wrapper),
:deep(.ant-input) {
  border-radius: 8px;
}

:deep(.ant-btn) {
  border-radius: 8px;
  height: 44px;
  font-weight: 500;
}
</style>
